/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using Comparison = System.Qizx.Util.Basic.Comparison;
namespace System.Qizx.Api.Util
{
	
	/// <summary> Representation of a Time value.
	/// 
	/// </summary>
	/// <author>  Xavier Franc. Most of this code borrowed from XMLmind XML Editor
	/// (courtesy of H. Shafie), and adapted (or botched-up).
	/// </author>
	[Serializable]
	public class XQTime:XQDateTimeBase
	{
		private const int DAY = 86400;
		
		public XQTime(int hour, int minute, double second, int tzSign, int tzHour, int tzMinute):base(EPOCH + 2, 12, 31, hour, minute, second, tzSign, tzHour, tzMinute)
		{
		}
		
		public XQTime(XQDateTimeBase dt):this(dt.hour, dt.minute, dt.second, dt.tzSign, dt.tzHour, dt.tzMinute)
		{
		}
		
		public XQTime(long epoch, int timezone):base(epoch % (DAY * 1000), timezone)
		{
		}
		
		public override XQDateTimeBase Parse(string that)
		{
			return parseTime(that);
		}
		
		public static XQTime parseTime(string s)
		{
			int hour;
			int minute;
			double second;
			int[] tz = new int[3];
			
			int charCount = s.Length;
			int pos = 0;
			if (pos + 2 >= charCount || s[pos + 2] != ':')
				throw new XQDateTimeException("invalid time syntax", s);
			hour = ParseInt(s, pos, pos + 2);
			pos += 3;
			
			if (pos + 2 >= charCount || s[pos + 2] != ':')
				throw new XQDateTimeException("invalid time syntax", s);
			minute = ParseInt(s, pos, pos + 2);
			pos += 3;
			
			int tzPos = ParseTimeZone(s, pos, tz);
			if (tzPos < pos + 2)
				throw new XQDateTimeException("invalid time syntax", s);
			
			second = ParseDouble(s, pos, tzPos);
			
			XQTime result = new XQTime(hour, minute, second, tz[0], tz[1], tz[2]);
			return result;
		}
		
		public override int CompareTo(XQDateTimeBase that)
		{
			double s1 = GetSecondsFromEpoch(), s2 = that.GetSecondsFromEpoch();
			if (s1 < 0)
				s1 += DAY;
			else if (s1 > DAY)
				s1 -= DAY;
			if (s2 < 0)
				s2 += DAY;
			else if (s2 > DAY)
				s2 -= DAY;
			return Comparison.of(s1, s2);
		}
		
		public override XQDateTimeBase Copy()
		{
			try
			{
				return new XQTime(hour, minute, second, tzSign, tzHour, tzMinute);
			}
			catch (XQDateTimeException cannotHappen)
			{
				return null;
			}
		}
		
		public override string ToString()
		{
			System.Text.StringBuilder buffer = new System.Text.StringBuilder();
			
			appendPadded(hour, 2, buffer);
			buffer.Append(':');
			appendPadded(minute, 2, buffer);
			buffer.Append(':');
			appendPadded(second, 2, buffer);
			
			appendTimeZone(tzSign, tzHour, tzMinute, buffer);
			
			return buffer.ToString();
		}
		
		public override XQDateTimeBase adjustToTimezone(int tzInMinutes, bool set_Renamed)
		{
			XQDateTimeBase result = base.adjustToTimezone(tzInMinutes, set_Renamed);
			normalize(result);
			return result;
		}
		
		public static void  normalize(XQDateTimeBase dt)
		{
			dt.year = 1972;
			dt.month = 12;
			dt.day = 31;
		}
	}
}